winsafe\gui\native_controls/
tab_item.rs

1use crate::co;
2use crate::decl::*;
3use crate::gui::*;
4use crate::msg::*;
5use crate::prelude::*;
6
7/// A single item of a [`Tab`](crate::gui::Tab) control.
8///
9/// **Note:** Each object keeps the zero-based index of an item. If new items
10/// are added/removed from the tab control, the object may then point to a
11/// different item.
12///
13/// You cannot directly instantiate this object, it is created internally by the
14/// control.
15#[derive(Clone, Copy)]
16pub struct TabItem<'a> {
17	owner: &'a Tab,
18	index: u32,
19}
20
21impl<'a> TabItem<'a> {
22	#[must_use]
23	pub(in crate::gui) const fn new(owner: &'a Tab, index: u32) -> Self {
24		Self { owner, index }
25	}
26
27	/// Returns the zero-based index of the item.
28	#[must_use]
29	pub const fn index(&self) -> u32 {
30		self.index
31	}
32
33	/// Deletes the item by sending a
34	/// [`tcm::DeleteItem`](crate::msg::tcm::DeleteItem) message.
35	///
36	/// # Safety
37	///
38	/// If you delete a tab automatically created, which has a container window
39	/// attached to it, the rendering will be out-of-order.
40	pub unsafe fn delete(&self) {
41		unsafe {
42			self.owner
43				.hwnd()
44				.SendMessage(tcm::DeleteItem { index: self.index })
45		}
46		.unwrap();
47	}
48
49	/// Retrieves the user-defined value by sending an
50	/// [`tcm::GetItem`](crate::msg::tcm::GetItem) message.
51	#[must_use]
52	pub fn lparam(&self) -> SysResult<isize> {
53		let mut tci = TCITEM::default();
54		tci.mask = co::TCIF::PARAM;
55
56		unsafe {
57			self.owner
58				.hwnd()
59				.SendMessage(tcm::GetItem { index: self.index, item: &mut tci })?;
60		}
61
62		Ok(tci.lParam)
63	}
64
65	/// Sets the user-defined value by sending an
66	/// [`lvm::SetItem`](crate::msg::lvm::SetItem) message.
67	///
68	/// Returns the same item, so further operations can be chained.
69	pub fn set_lparam(&self, lparam: isize) -> SysResult<Self> {
70		let mut tci = TCITEM::default();
71		tci.mask = co::TCIF::PARAM;
72		tci.lParam = lparam;
73
74		unsafe {
75			self.owner
76				.hwnd()
77				.SendMessage(tcm::SetItem { index: self.index, item: &mut tci })?;
78		}
79		Ok(*self)
80	}
81
82	/// Sets the text by sending a
83	/// [`tcm:SetItem`](crate::msg::tcm::SetItem) message.
84	///
85	/// Returns the same item, so further operations can be chained.
86	pub fn set_text(&self, text: &str) -> SysResult<Self> {
87		let mut wtext = WString::from_str(text);
88		let mut tci = TCITEM::default();
89		tci.mask = co::TCIF::TEXT;
90		tci.set_pszText(Some(&mut wtext));
91
92		unsafe {
93			self.owner
94				.hwnd()
95				.SendMessage(tcm::SetItem { index: self.index, item: &mut tci })?;
96		}
97		Ok(*self)
98	}
99
100	/// Retrieves the text by sending a
101	/// [`tcm:GetItem`](crate::msg::tcm::GetItem) message.
102	#[must_use]
103	pub fn text(&self) -> SysResult<String> {
104		let mut buf = WString::new_alloc_buf(64); // arbitrary
105		let mut tci = TCITEM::default();
106		tci.mask = co::TCIF::TEXT;
107		tci.set_pszText(Some(&mut buf));
108
109		unsafe {
110			self.owner
111				.hwnd()
112				.SendMessage(tcm::GetItem { index: self.index, item: &mut tci })?;
113		}
114		Ok(buf.to_string())
115	}
116}